<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>{{ i18n('business.activation.code.from') }}</div>
        </div>

        <div class="admin_main_block_right">
          <div>
            <el-button icon="Back" @click="router.go(-1)">
              {{ i18n('common.return') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_form_main">
        <el-form label-width="100px" ref="formRef" :model="dataForm" :rules="dataRule">
          <el-form-item :label="i18n('business.activation.code.prefix')" prop="prefix">
            <el-input v-model="dataForm.prefix" :placeholder="i18n('common.please_input') + i18n('business.activation.code.prefix')" show-word-limit maxlength="2" clearable/>
          </el-form-item>

          <el-form-item :label="i18n('business.activation.code.face_value')" prop="face_value">
            <el-input-number v-model="dataForm.face_value" :min="1">
            </el-input-number>
          </el-form-item>

          <el-form-item :label="i18n('business.activation.code.valid_time')" prop="valid_time">
            <el-date-picker
              v-model="dataForm.valid_time"
              type="daterange" :disabled-date="disabledDate"
              :range-separator="i18n('common.to')" :start-placeholder="i18n('common.start_time')" :end-placeholder="i18n('common.end_time')" clearable/>
          </el-form-item>

          <el-form-item :label="i18n('business.activation.code.total')" prop="total">
            <el-input-number v-model="dataForm.total" :min="1">
            </el-input-number>
          </el-form-item>

          <el-form-item>
            <el-button
              type="primary"
              @click="dataFormSubmit(formRef)">
              {{ i18n('common.confirm') }}
            </el-button>

            <el-button @click="resetForm(formRef)">
              {{ i18n('common.reset') }}
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup name="activation-code-form">
  import { useBase } from "@/hooks/base/useBase"
  import { useForm } from "@/hooks/form/useForm"
  import { isEmpty } from '@/utils/validate'

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('business/activation/code')

  const dataForm = reactive({
    id: 0,
    prefix: '',
    face_value: '',
    valid_time: '',
    total: 1,
  })

  const dataRule = reactive({
    prefix: [{
      required: true,
      message: i18n('business.activation.code.rules.prefix.require'),
      trigger: 'blur'
    }],
    valid_time: [{
      required: true,
      message: i18n('business.activation.code.rules.valid_time.require'),
      trigger: 'blur'
    }],
    total: [{
      required: true,
      message: i18n('business.activation.code.rules.total.require'),
      trigger: 'blur'
    }]
  })

  const {
    formRef,
    dataFormSubmit,
    resetForm
  } = useForm(model.value, dataForm)

  const init = (form) => {
    dataForm.id = query.id || 0

    nextTick(() => {
      form.value.resetFields()
      if (dataForm.id) {
        $http({
          url: $http.adornUrl(`/${model.value}/view/${dataForm.id}`),
          method: 'get',
          params: $http.adornParams({}),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            dataForm.id = data.data.id
            dataForm.prefix = data.data.prefix
            dataForm.face_value = data.data.face_value
            dataForm.valid_time = data.data.valid_time
            dataForm.total = data.data.total
          }
        })
      }
    })
  }

  const loadFaceValue = () => {
    $http({
      url: $http.adornUrl(`/config/data`),
      method: 'get',
      params: $http.adornParams({
        title: 'exchange_face_value'
      }),
    }).then(({ data }) => {
      if (data && data.status === 200) {
        dataForm.face_value = data.data.value
      }
    })
  }

  const loadValueTime = () => {
    $http({
      url: $http.adornUrl(`/${model.value}/valid`),
      method: 'get',
      params: $http.adornParams({}),
    }).then(({ data }) => {
      if (data && data.status === 200) {
        dataForm.valid_time = data.data
      }
    })
  }

  const disabledDate = (time) => {
    return time.getTime() + 24 * 60 * 60 * 1000 < Date.now()
  }


  onMounted(() => {
    init(formRef)

    loadFaceValue()
    loadValueTime()
  })
</script>
